package com.r2coding.system.controller;

import java.util.List;

import com.r2coding.common.enums.Module;
import com.r2coding.common.enums.OperationType;
import com.r2coding.common.util.PageUtils;
import com.r2coding.common.util.Result;
import com.r2coding.system.annotation.SysLog;
import com.r2coding.system.vo.req.AddUserRoleReqVo;
import com.r2coding.system.vo.req.QueryRoleVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import com.r2coding.system.entity.RoleEntity;
import com.r2coding.system.service.RoleService;

/**
 * 
 *
 * @author 98k灬
 * @email hk666zx@163.com
 * @date 2022-04-04 11:10:29
 */
@RestController
@RequestMapping("/role")
@Slf4j
public class RoleController {
    @Autowired
    private RoleService roleService;

    /**
     * 列表
     */
    @PostMapping("/list")
    public Result list(@RequestBody QueryRoleVo vo){
        PageUtils page = roleService.queryAll(vo);
        return Result.ok(page);
    }

    //@Cacheable(value = "role", key = " #root.methodName ")
    @GetMapping("/getAll")
    public Result getAll() {
        List<RoleEntity> list = roleService.list();
        return Result.ok(list);
    }

    /**
     * 给用户分配角色信息
     *
     * @param vo 请求体
     * @return 统一响应体
     * @throws Exception 异常信息
     */
    @SysLog(module = Module.SYSTEM, operationType = OperationType.INSERT, description = "分配角色")
    @PreAuthorize("hasAuthority('role:add')")
    @PostMapping("/addUserRole")
    public Result addUserRole(@RequestBody AddUserRoleReqVo vo) throws Exception {
        roleService.addUserRole(vo);
        return Result.ok();
    }

    /**
     * 信息
     */
    //@Cacheable(value = "role", key = " #root.methodName + '_' + #roleId")
    @GetMapping("/info/{roleId}")
    public Result info(@PathVariable("roleId") Integer roleId) {
		RoleEntity role = roleService.getById(roleId);
        return Result.ok(role);
    }

    /**
     * 根据角色id获取权限信息
     *
     * @param roleId 角色id
     * @return 权限信息
     */
    @GetMapping("/getPermissionIdsByRoleId/{roleId}")
    public Result getPermissionIdsByRoleId(@PathVariable("roleId") Integer roleId){
        List<Integer> ids = roleService.getPermissionIdsByRoleId(roleId);
        return Result.ok(ids);
    }

    /**
     * 保存
     */
    @SysLog(module = Module.SYSTEM, operationType = OperationType.INSERT, description = "保存角色")
    @PreAuthorize("hasAuthority('role:add')")
    @PostMapping("/save")
    public Result save(@RequestBody RoleEntity role){
		roleService.saveRole(role);
        return Result.ok();
    }

    /**
     * 修改
     */
    @SysLog(module = Module.SYSTEM, operationType = OperationType.UPDATE, description = "修改角色")
    @PreAuthorize("hasAuthority('role:edit')")
    @PostMapping("/update")
    public Result update(@RequestBody RoleEntity role){
        System.out.println("role:" +role);
		roleService.updateById(role);
        return Result.ok();
    }

    /**
     * 删除
     */
    @SysLog(module = Module.SYSTEM, operationType = OperationType.DELETE, description = "删除角色")
    @PreAuthorize("hasAuthority('role:delete')")
    @DeleteMapping("/delete/{roleId}")
    public Result delete(@PathVariable("roleId") Integer roleId){
		roleService.removeRole(roleId);
        return Result.ok();
    }

}
